package top.cdmrx.demo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import top.cdmrx.demo.edu.entity.Video;
import top.cdmrx.demo.edu.mapper.VideoMapper;
import top.cdmrx.demo.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.cdmrx.demo.edu.client.VodClient;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public boolean getCountByChapterId(String chapterId) {
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id",chapterId);
        Integer integer = baseMapper.selectCount(videoQueryWrapper);

        return null!=integer && integer>0;
    }

    /**
     * 根据课程id删除小节
     * @param id
     */
    @Override
    public void removeId(String id) {
        QueryWrapper<Video> videoQueryWrapper1 = new QueryWrapper<>();
        videoQueryWrapper1.eq("course_id",id);
        videoQueryWrapper1.select("video_source_id");
        List<Video> videoList = baseMapper.selectList(videoQueryWrapper1);

        //转换，将List<Video>转为List<String>
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i <videoList.size(); i++) {
            Video video = videoList.get(i);
            String videoSourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){

                list.add(videoSourceId);
            }
        }

        //当多个视频的id集合有值才进行删除
        if (list.size()>0){

            vodClient.deleteBatch(list);
        }


        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",id);
        baseMapper.delete(videoQueryWrapper);
    }
}
